package Algorithm;

public class Num733 {
    int curColor,c;
    int[] dx={1,0,0,-1};
    int[] dy={0,-1,1,0};
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        curColor=image[sr][sc];
        c=newColor;
        if(curColor==c) return image;
        else dfs(image,sr,sc);
        return image;
    }
    private void dfs(int[][] image,int sr,int sc){
        if(sr<0 || sr>=image.length || sc<0 || sc>=image[0].length || image[sr][sc]==c || image[sr][sc]!=curColor) return;
        image[sr][sc]=c;
        for(int i=0;i<4;i++){
            int x=sr+dx[i];
            int y=sc+dy[i];
            dfs(image,x,y);
        }
    }
}
